package com.example.tests;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import static com.example.tests.GroupDataGenerator.generateRandomGroups;
import static com.example.tests.AddressDataGenerator.generateRandomAddresses;

public class TestBase {
	
	protected static ApplicationManager app;
	private String configFile;

	
	@BeforeTest
	public void setUp() throws Exception {
		configFile = System.getProperty("configFile", "application.properties");
		Properties properties = new Properties();
		properties.load(new FileReader(new File (configFile)));
		app = new ApplicationManager(properties);	  
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		app.stop();
	   
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator(){
		return wrapGroupsForDataProvider(generateRandomGroups(3)).iterator();
	}
	
	public static List<Object[]> wrapGroupsForDataProvider(List<GroupData> groups) {
		List <Object[]> list = new ArrayList<Object[]>();
		for (GroupData group : groups) {
			list.add(new Object[]{group});
		}
		return list;
	}

/*	public String generateRandomString(){        // this method should be deleted
		Random rnd = new Random();
		if(rnd.nextInt(3) == 0){
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
*/	
	@DataProvider
	public Iterator<Object[]> randomValidAddressGenerator(){
		return wrapAddressesForDataProvider(generateRandomAddresses(3)).iterator();
	}

	public List<Object[]> wrapAddressesForDataProvider(List<AddressData> addresses) {
		List <Object[]> list = new ArrayList<Object[]>();
		for (AddressData address : addresses) {
			list.add(new Object[]{address});
		}
		return list;
	}
	
	// always returns something
/*	public String generateBDay(){
		Random rnd = new Random();
		String string="";
		int bday = rnd.nextInt(31);
		if (bday == 0){
			return "-";
		} else {
			string = string + bday;
			return string;
		}
		
	}
	
	public String generateBMonth(){
		Random rnd = new Random();
		int stringNumber = rnd.nextInt(12);
		String month = "";
		switch(stringNumber){
			case 0: month = "-"; break;
			case 1: month = "January"; break;
			case 2: month = "February"; break;
			case 3: month = "March"; break;
			case 4: month = "April"; break;
			case 5: month = "May"; break;
			case 6: month = "June"; break;
			case 7: month = "July"; break;
			case 8: month = "August"; break;
			case 9: month = "September"; break;
			case 10: month = "October"; break;
			case 11: month = "November"; break;
			case 12: month = "December"; break;
		}
		return month;
	}
	
	public String generateBYear(){
		Random rnd = new Random();
		String string="";
		int byear = rnd.nextInt(2013);
		if (byear == 0){
			return "-";
		} else {
			string = string + byear;
			return string;
		}
		
	}
*/
}
